package com.aplink.generic.widgets.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.aplink.sg.R;

public class Gallery extends FrameLayout {
	private HackyViewPager mGallery;
	private ImageButton mBtnTakePicture;
	private ImageButton mBtnDeletePicture;
	private ImageButton mBtnScanBarcode;

	public Gallery(Context context) {
		super(context);
		init(null, 0);
	}

	public Gallery(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(attrs, 0);
	}

	public Gallery(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(attrs, defStyle);
	}

	private void setButtonTransparent(ImageButton imageButton) {
		if (!isInEditMode()) {
			imageButton
					.setBackgroundResource(R.drawable.aplink__generic_button_transparent);
		}
	}

	private void init(AttributeSet attrs, int defStyle) {
		mGallery = new HackyViewPager(getContext());
		mGallery.setLocked(false);
		mGallery.setId(0x101);
		mBtnTakePicture = new ImageButton(getContext());
		setButtonTransparent(mBtnTakePicture);
		mBtnDeletePicture = new ImageButton(getContext());
		setButtonTransparent(mBtnDeletePicture);
		mBtnScanBarcode = new ImageButton(getContext());
		setButtonTransparent(mBtnScanBarcode);

		LinearLayout linearLayout = new LinearLayout(getContext());
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		linearLayout.setBackgroundColor(Color.parseColor("#80F0F0F0"));
		linearLayout.setGravity(Gravity.RIGHT);
		LinearLayout.LayoutParams imageButtonLayoutParams = new LinearLayout.LayoutParams(
				(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
						48, getResources().getDisplayMetrics()),
				(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 0,
						getResources().getDisplayMetrics()));
		imageButtonLayoutParams.weight = 1f;
		linearLayout.addView(mBtnTakePicture, imageButtonLayoutParams);
		linearLayout.addView(mBtnDeletePicture, imageButtonLayoutParams);
		linearLayout.addView(mBtnScanBarcode, imageButtonLayoutParams);
		addView(mGallery);

		LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.MATCH_PARENT);
		layoutParams.gravity = Gravity.RIGHT;
		linearLayout.setLayoutParams(layoutParams);
		addView(linearLayout);
		initAttributeSet(attrs);
	}

	private void initAttributeSet(AttributeSet attrs) {
		TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
				R.styleable.Gallery, 0, 0);
		Drawable buttonTakePicture = typedArray
				.getDrawable(R.styleable.Gallery_buttonTakePicture);
		if (buttonTakePicture != null) {
			mBtnTakePicture.setImageDrawable(buttonTakePicture);
		} else {
			mBtnTakePicture.setVisibility(View.GONE);
		}
		// Drawable buttonOpenGallery = typedArray
		// .getDrawable(R.styleable.Gallery_buttonOpenGallery);
		// if (buttonOpenGallery != null) {
		// mBtnOpenGallery.setImageDrawable(buttonOpenGallery);
		// } else {
		// mBtnOpenGallery.setVisibility(View.GONE);
		// }
		Drawable buttonDeletePhoto = typedArray
				.getDrawable(R.styleable.Gallery_buttonDeletePhoto);
		if (buttonDeletePhoto != null) {
			mBtnDeletePicture.setImageDrawable(buttonDeletePhoto);
		} else {
			mBtnDeletePicture.setVisibility(View.GONE);
		}
		Drawable buttonScanBarcode = typedArray
				.getDrawable(R.styleable.Gallery_buttonScanBarcode);
		if (buttonScanBarcode != null) {
			mBtnScanBarcode.setImageDrawable(buttonScanBarcode);
		} else {
			mBtnScanBarcode.setVisibility(View.GONE);
		}
		typedArray.recycle();
	}

	public ViewPager getGallery() {
		return mGallery;
	}

	public ImageButton getBtnTakePicture() {
		return mBtnTakePicture;
	}

	public void setBtnTakePicture(ImageButton btnTakePicture) {
		mBtnTakePicture = btnTakePicture;
	}

	public ImageButton getBtnDeletePicture() {
		return mBtnDeletePicture;
	}

	public void setBtnDeletePicture(ImageButton btnDeletePicture) {
		mBtnDeletePicture = btnDeletePicture;
	}

	public ImageButton getBtnScanBarcode() {
		return mBtnScanBarcode;
	}

	public void setBtnScanBarcode(ImageButton btnScanBarcode) {
		mBtnScanBarcode = btnScanBarcode;
	}

	public class HackyViewPager extends ViewPager {

		private boolean isLocked;

		public HackyViewPager(Context context) {
			super(context);
			isLocked = false;
		}

		public HackyViewPager(Context context, AttributeSet attrs) {
			super(context, attrs);
			isLocked = false;
		}

		@Override
		public boolean onInterceptTouchEvent(MotionEvent ev) {
			if (!isLocked) {
				try {
					return super.onInterceptTouchEvent(ev);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
					return false;
				}
			}
			return false;
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			if (!isLocked) {
				return super.onTouchEvent(event);
			}
			return false;
		}

		public void toggleLock() {
			isLocked = !isLocked;
		}

		public void setLocked(boolean isLocked) {
			this.isLocked = isLocked;
		}

		public boolean isLocked() {
			return isLocked;
		}

	}

	public void setCurrentItem(int item, boolean smoothScroll) {
		mGallery.setCurrentItem(item, smoothScroll);
	}

	public void setAdapter(PagerAdapter pagerAdapter) {
		mGallery.setAdapter(pagerAdapter);
	}

	public void setOffscreenPageLimit(int limit) {
		mGallery.setOffscreenPageLimit(limit);
	}

	public int getCurrentItem() {
		return mGallery.getCurrentItem();
	}

	public void setTakePictureClick(OnClickListener onClickListener) {
		mBtnTakePicture.setOnClickListener(onClickListener);
	}

	public void setDeletePictureClick(OnClickListener onClickListener) {
		mBtnDeletePicture.setOnClickListener(onClickListener);
	}

	public void setScanBarcodeClick(OnClickListener onClickListener) {
		mBtnScanBarcode.setOnClickListener(onClickListener);
	}
}
